package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.auth.login.annotation.NeedLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	// Request URL: http://localhost:8500/api/account/userAccount/getAvailableAmount
	@GetMapping("getAvailableAmount")
	@Operation(summary = "获取用户账户余额")
	@NeedLogin
	public Result getAvailableAmount() {
		BigDecimal availableAmount = userAccountService.getAvailableAmount();
		return Result.ok(availableAmount);
	}

	// Request URL: http://localhost:8500/api/account/userAccount/findUserRechargePage/1/10
	@NeedLogin
	@Operation(summary = "获取用户充值记录")
	@GetMapping("/findUserRechargePage/{page}/{limit}")
	public Result findUserRechargePage(
			@Parameter(name = "page", description = "当前页码", required = true)
			@PathVariable Long page,

			@Parameter(name = "limit", description = "每页记录数", required = true)
			@PathVariable Long limit) {
		// 获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		// 构建Page对象
		IPage<UserAccountDetail> pageParam = new Page<>(page, limit);
		// 调用服务层方法
		IPage<UserAccountDetail> pageModel = userAccountService.findUserRechargePage(pageParam, userId);
		// 返回数据
		return Result.ok(pageModel);
	}

	// Request URL: http://localhost:8500/api/account/userAccount/findUserConsumePage/1/10
	@NeedLogin
	@Operation(summary = "获取用户充值记录")
	@GetMapping("/findUserConsumePage/{page}/{limit}")
	public Result findUserConsumePage(
			@Parameter(name = "page", description = "当前页码", required = true)
			@PathVariable Long page,

			@Parameter(name = "limit", description = "每页记录数", required = true)
			@PathVariable Long limit) {
		// 获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		// 构建Page对象
		IPage<UserAccountDetail> pageParam = new Page<>(page, limit);
		// 调用服务层方法
		IPage<UserAccountDetail> pageModel = userAccountService.findUserConsumePage(pageParam, userId);
		// 返回数据
		return Result.ok(pageModel);
	}
}

